const notEmpty = function() {
    return function(name) {
        return !name || name.trim() === '' ? '该项不能为空' : true
    }
}
// 中划线转大驼峰
const toUpperCase = (str) => str.split('-').map(item => item.charAt(0).toUpperCase() + item.slice(1)).join('')

module.exports = function (plop) {
    plop.setGenerator('createModule', {
        description: '只创建api和model',
        prompts: [
            {
                type: 'input',
                name: 'name',
                message: '请输入模块名称',
                validate: notEmpty('name')
            },
            {
                type: 'input',
                name: 'baseUrl',
                message: '请输入api请求基础路径，以/开头',
                default: '/example'
            }
        ],
        actions: function(data) {
            const { name, baseUrl } = data
            const upperCaseName = toUpperCase(name)
            const apiName = upperCaseName + 'Api'
            const modelName = upperCaseName + 'Model'
            const actions = [
                {
                    type: 'add',
                    path: '../../src/api/modules/{{name}}.js',
                    templateFile: '../template/api.hbs',
                    data: {
                        apiName,
                        baseUrl
                    }
                },
                {
                    type: 'add',
                    path: '../../src/models/modules/{{name}}.js',
                    templateFile: '../template/model.hbs',
                    data: {
                        apiName,
                        modelName,
                    }
                }
            ]
            console.log(modelName)
            return actions
        }
    })
}
